package com.jg.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.jg.entity.EmployeeProfiles;
import com.jg.service.EmployeeProfilesService;
import com.jg.service.UserService;
import com.jg.service.impl.EmployeeProfilesServiceImpl;
import com.jg.service.impl.UserServiceImpl;
import com.jg.util.Page;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 86130
 * @version 1.0
 * @description: TODO
 * @date 2025/3/25 19:06
 */
@WebServlet("/user.action")
public class UserServlet extends HttpServlet {
    EmployeeProfilesService employeeProfilesService = new EmployeeProfilesServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        String action = req.getParameter("action") == null ? "" : req.getParameter("action");
        switch (action) {
            case "edit":
                empEdit(req, resp);
                break;
            case "detail":
                empDetail(req, resp);
                break;
            case "delete":
                empDel(req, resp);
                break;
            case "list":
                emplist(req, resp);
                break;
        }
    }

    private void empDetail(HttpServletRequest req, HttpServletResponse resp) {
        String id = req.getParameter("id");
        EmployeeProfiles employeeProfilesById = employeeProfilesService.findEmployeeProfilesById(Integer.valueOf(id));
        req.setAttribute("emp",employeeProfilesById);
    }

    private void empEdit(HttpServletRequest req, HttpServletResponse resp) {
        UserService userService = new UserServiceImpl();
        String id = req.getParameter("id");
        String userId = req.getParameter("userId");
        String name = req.getParameter("name");
        String position = req.getParameter("position");
        String department = req.getParameter("department");
        String hireDate = req.getParameter("hireDate");
        String leaveDate = req.getParameter("leaveDate");
        String status = req.getParameter("status");
        EmployeeProfiles employeeProfiles = new EmployeeProfiles();
        if (id != null){
            employeeProfiles.setId(Integer.valueOf(id));
            employeeProfiles.setUserId(Integer.valueOf(userId));
            employeeProfiles.setName(name);
            employeeProfiles.setPosition(position);
            employeeProfiles.setDepartment(Integer.parseInt(department));
//            employeeProfiles.setHireDate(hireDate);
//            employeeProfiles.setLeaveDate(leaveDate);
            employeeProfiles.setStatus(status);
        }else{
            employeeProfiles.setId(Integer.valueOf(id));
            employeeProfiles.setUserId(Integer.valueOf(userId));
            employeeProfiles.setName(name);
            employeeProfiles.setPosition(position);
            employeeProfiles.setDepartment(Integer.parseInt(department));
//            employeeProfiles.setHireDate(hireDate);
//            employeeProfiles.setLeaveDate(leaveDate);
            employeeProfiles.setStatus(status);
            userService.addUser(employeeProfiles);
        }
    }

    private void empDel(HttpServletRequest req, HttpServletResponse resp) {
    }


    private void emplist(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Page page = new Page();
        String currentPage = req.getParameter("currentPage");
        if (currentPage != null && !"0".equals(currentPage)){
            page.setCurrentPage(Integer.parseInt(currentPage));
        }
        if (currentPage != null){
            page.setCurrentPage(Integer.parseInt(currentPage));
        }
        page = employeeProfilesService.findEmployeeProfilesAll(page);
        String json = JSON.toJSONStringWithDateFormat(page,"yyyy-MM-dd", SerializerFeature.WriteNullStringAsEmpty);
        resp.getWriter().write(json);
    }
}
